package com.cipe.cmrs.controllers;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cipe.cmrs.model.ActionableAlert;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.model.ref.LinkCategory;
import com.cipe.cmrs.service.ActionableAlertService;
import com.cipe.cmrs.service.ChangeNotificationService;
import com.cipe.cmrs.service.DashBoardService;
import com.cipe.cmrs.service.UserService;
import com.cipe.cmrs.service.ref.EntityRefService;

@Controller
public class DashboardController extends BaseController {

	private static final long	serialVersionUID	= 2953245877386975346L;
	// REplace this with "dashboard2" to see the new ui
	private String formView = "dashboard";
	
	@Autowired
	private UserService			userService;
	@Autowired
	private EntityRefService	entityRefService;
	
	@Autowired
	private DashBoardService    dashBoardService;
	
	@Autowired
	ActionableAlertService actionableAlertService;
	
	@Autowired
	ChangeNotificationService changeNotificationService;

	@RequestMapping("/secure/changePassword.do")
	public String changePassword(@ModelAttribute User user, BindingResult result, Model model, HttpServletRequest request) {
		try {
			userService.changePassword(user);
			saveMessage(request, getText("changePassword.message.success"));
		} catch (Exception e) {
			logger.error(getText("error.transaction"), e);
			saveError(request, getText("error.transaction"));
		}

		user.setPassword(null);
		user.setConfirmPassword(null);
		user.setNewPassword(null);
		model.addAttribute("user", user);
		return "changePassword";
	}

	@RequestMapping("/secure/manageAccountCodes.do")
	public String manageAccountCodes() {
		return "manageAccountCodes";
	}
    
	@RequestMapping("/secure/checkSession.do")
	public void checkSession(HttpServletRequest request, HttpServletResponse resp) throws IOException{
		//System.out.println("checkSession....");
		HttpSession session = request.getSession();
		if (session.getAttribute(User.LOGGED_IN_USER_KEY) != null) {
		 resp.getWriter().write("success");
		} else {resp.getWriter().write("failed");}
		
	}
	
	@RequestMapping("/secure/manageFieldOffices.do")
	public String manageFieldOffices() {
		return "manageFieldOffices";
	}

	@RequestMapping("/secure/manageRegionCountriesAndStates.do")
	public String manageRegionCountriesAndStates() {
		return "manageRegionCountriesAndStates";
	}

	// @RequestMapping("/secure/addDonorAward.do")
	// public String addDonorAward() {
	// return "addDonorAward";
	// }
	@RequestMapping("/secure/manageUsersAndRoles.do")
	public String manageUsersAndRoles() {
		return "manageUsersAndRoles";
	}

	@RequestMapping("/secure/processPaymentReq.do")
	public String processPaymentReq() {
		return "processPaymentReq";
	}

	@RequestMapping("/secure/refreshDashboard.do")
	public String refreshDashboard(Model model,HttpServletRequest request) {
		model.addAttribute("dashBoardCountList",dashBoardService.getAllDashboarBoardLinks());
		model.addAttribute("ActionableAlertList",actionableAlertService.getAllActionableAlert());
		model.addAttribute("actionableAlert",new ActionableAlert());
		User user = userService.findById(getUserId(request));
		model.addAttribute("ChangeNotificationList", changeNotificationService.getAllChangeNotification(user));
		return "dashboardDiv";
	}
	
	@RequestMapping("/secure/changeNotification.do")
	public String changeNotification(@RequestParam("id") int id,Model model,HttpServletRequest request) {
		User user = userService.findById(getUserId(request));
		changeNotificationService.chnageNotificationPeference(user, id);
		model.addAttribute("ChangeNotificationList", changeNotificationService.getAllChangeNotification(user));
		return "dashboardCN";
	}

	// @RequestMapping("/secure/donorAwardList.do")
	// public String donorAwardList() {
	// return "donorAwardList";
	// }
	// @RequestMapping("/secure/showAddProject.do")
	// public String showAddProject(){
	// return "editProject";
	// }
	//
	// @RequestMapping("/secure/showProjectList.do")
	// public String showProjectList() {
	// return "projectList";
	// }
	@RequestMapping("/secure/reportComputerProblem.do")
	public String reportComputerProblem() {
		return "reportComputerProblem";
	}

	// Partner Monitoring
	@RequestMapping("/secure/searchAudit.do")
	public String searchAudit() {
		return "searchAudit";
	}

	@RequestMapping("/secure/searchDonor.do")
	public String searchDonor() {
		return "searchDonor";
	}

	@RequestMapping("/secure/searchDonorAward.do")
	public String searchDonorAward() {
		return "searchDonorAward";
	}

	@RequestMapping("/secure/searchPartner.do")
	public String searchPartner() {
		return "searchPartner";
	}

	@RequestMapping("/secure/searchPaymentProcessing.do")
	public String searchPaymentProcessing() {
		return "searchPaymentProcessing";
	}

	@RequestMapping("/secure/searchProject.do")
	public String searchProject() {
		return "searchProject";
	}

//	// Payment Processing
//	@RequestMapping("/secure/showAddAudit.do")
//	public String showAddAudit() {
//		return "editAudits";
//	}

	@RequestMapping("/secure/showAddFinNarRepReq.do")
	public String showAddFinNarRepReq() {
		return "addFinNarRepReq";
	}

	@RequestMapping("/secure/showAddPartnerMonDetail.do")
	public String showAddPartnerMonDetail() {
		return "addPartnerMonDetail";
	}

	@RequestMapping("/secure/showAddPaymentProcessing.do")
	public String showAddPaymentProcessing() {
		return "editPaymentProcessing";
	}

	@RequestMapping("/secure/showAuditList.do")
	public String showAuditList() {
		return "auditList";
	}

	// Knowledge Management
	/*
	 * @RequestMapping("/secure/showAddKnowledgeManagement.do") public String
	 * showAddKnowledgeManagement(){ return "editKnowledgeManagement"; }
	 * @RequestMapping("/secure/showKnowledgeManagementList.do") public String
	 * showKnowledgeManagementList() { return "knowledgeManagementList"; }
	 */
	// Reports
	@RequestMapping("/secure/showChangePassword.do")
	public String showChangePassword(HttpServletRequest request, Model model) {
		User loggedInUser = userService.findById(getUserId(request));
		User user = new User();
		user.setLoginName(loggedInUser.getLoginName());
		model.addAttribute("user", user);
		return "changePassword";
	}

	// Contact Us
	@RequestMapping("/secure/showEditFinNarRepReq.do")
	public String showEditFinNarRepReq() {
		return "editFinNarRepReq";
	}

	@RequestMapping("/secure/showEditPartnerMonDetail.do")
	public String showEditPartnerMonDetail() {
		return "editPartnerMonDetail";
	}

	@RequestMapping("/secure/showEmailGrantManagement.do")
	public String showEmailGrantManagement() {
		return "emailGrantManagement";
	}

	// System Administration
	@RequestMapping("/secure/showInstructionsPaymentReq.do")
	public String showInstructionsPaymentReq() {
		
		return "instructionsPaymentReq";
	}

	@RequestMapping("/secure/showDashboard.do")
	public String showLogin(HttpServletRequest request, Model model) {
		User user = userService.findById(getUserId(request));
		if (user == null) {
			return "redirect:/../login.do";
		}
		model.addAttribute("user", user);
		model.addAttribute("linkCategories", entityRefService.findAllByType(LinkCategory.class));
		model.addAttribute("dashBoardCountList",dashBoardService.getAllDashboarBoardLinks());
		model.addAttribute("ActionableAlertList",actionableAlertService.getAllActionableAlert());
		model.addAttribute("ChangeNotificationList", changeNotificationService.getAllChangeNotification(user));
		model.addAttribute("actionableAlert",new ActionableAlert());
		return formView;
	}

	@RequestMapping("/secure/showPageHeading.do")
	public String showPageHeading(@RequestParam String heading, Model model) {
		model.addAttribute("heading", heading);
		return "headingPage";
	}

	@RequestMapping("/secure/showPartnerMonDetail.do")
	public String showPartnerMonDetail() {
		return "partnerMonDetail";
	}

	// Audit
	@RequestMapping("/secure/showPaymentProcessingList.do")
	public String showPaymentProcessingList() {
		return "paymentProcessingList";
	}
	@RequestMapping("/secure/showReportList.do")
	public String showReportList() {
		return "reportList";
	}

	@RequestMapping("/secure/systemReq.do")
	public String systemReg() {
		return "systemReg";
	}
}
